//
//  WHYueViewController.m
//  WHstoreItemProtect
//
//  Created by wztx on 2017/11/5.
//  Copyright © 2017年 sdwh.com. All rights reserved.
//

#import "WHYueViewController.h"

#import "LWGroupItem.h"
#import "LWSettingItem.h"

#import "Masonry.h"

@interface WHYueViewController ()

//当前界面的数据源 用来存储分组模型
@property (nonatomic ,strong) NSMutableArray *groupArray;

@end

@implementation WHYueViewController

#pragma MARK - 懒加载
- (NSMutableArray *)groupArray
{
    if (!_groupArray) {
        _groupArray = [NSMutableArray array];
    }
    return _groupArray;
}
//构造
- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"余额";
    
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(popClick)];
    self.navigationItem.leftBarButtonItem = backBtnItem;
    
    [self setupUI];
    [self group1];
}
#pragma MARK - POP
- (void)popClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma MARK - UI
- (void)setupUI
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 140)];
    headerView.backgroundColor = [UIColor magentaColor];
    
    /** 账户余额 */
    UILabel *accountLabel = [UILabel new];
    accountLabel.text = @"账户余额(元)";
    accountLabel.textColor = [UIColor blackColor];
    [headerView addSubview:accountLabel];
    
    /** 账户金额 */
    UILabel *yueLabel = [UILabel new];
    yueLabel.text = @"8888 元";
    yueLabel.textColor = [UIColor blackColor];
    [headerView addSubview:yueLabel];

    
    [accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView.mas_left).offset(20);
        make.top.equalTo(headerView.mas_top).offset(40);
    }];
    
    [yueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(accountLabel);
        make.top.equalTo(accountLabel.mas_bottom).offset(10);
    }];
    
    
    self.tableView.tableHeaderView = headerView;
}

#pragma MARK - 条目分组
- (void)group1
{
    LWGroupItem *group0 = [[LWGroupItem alloc] init];
    LWSettingItem *item = [LWSettingItem itemWithTitle:@"充值" icon:@"chongzhi"];
    LWSettingItem *item1 = [LWSettingItem itemWithTitle:@"提现" icon:@"tixian"];
    LWSettingItem *item2 = [LWSettingItem itemWithTitle:@"转账" icon:@"zhuanzhang"];
    
    group0.itemsArray = @[item,item1,item2];
    
    [self.groupArray addObject:group0];
    
}
/**
 *  返回有多少组的代理方法
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.groupArray.count;
}

/**
 *  返回每组有多少行的代理方法
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    LWGroupItem *group = self.groupArray[section];
    return group.itemsArray.count;
}

/**
 *  返回每一行Cell的代理方法
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell  = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    LWGroupItem *group = self.groupArray[indexPath.section];
    LWSettingItem *item = group.itemsArray[indexPath.row];
    cell.textLabel.text = item.title;
    cell.imageView.image = [UIImage imageNamed:item.icon];
    return cell;
}
/**
 *  选中每一行Cell的代理方法
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        NSLog(@"充值");
    }else if (indexPath.row == 1)
    {
        NSLog(@"提现");
    }else
    {
        NSLog(@"转账");
    }
}

@end
